﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Xml.Serialization;
using System.IO;
namespace MAP_EDITOR_AHNT
{
    public partial class Form1 : Form
    {
        private Bitmap Background;
        private Bitmap background;
        private Graphics draw_bitmap;
        private Graphics draw_background;
        private int mousex;
        private int mousey;
        private int mapwidth=640;
        private NewMap newmap = new NewMap();
        public ArrayList listObject = new ArrayList();
       // public Object[] listObject= new Object[200];
        
       // public int nObject = 0;
        private string filename = "Hinh\\ho.png";
        public int id;
        public int kind;
        public int width;
        public int height;
        public int curposition=0;
        public bool isdelete = false;
        private bool ismousein;
        private bool isdrawed=true;
        public Form1()
        {
            InitializeComponent();
            Background = new Bitmap(640,480);
            draw_bitmap = Graphics.FromImage(Background);
            draw_background = PanelBackground.CreateGraphics();
        }
       
        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

       

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
           //NewMap newmap = new NewMap();
           // newmap.ShowDialog();
            timer1.Enabled = false;
            NewMap();
        }


        private void pictureNuiNho_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureNuiNho.Image;
            kind = 0;
            id = 0;
            filename = "Hinh\\nuinho.gif";
        }
        private void pictureNuiTo_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureNuiTo.Image;
            kind = 1;
            filename = "Hinh\\nuito.gif";
            id = 0;
        }
        private void pictureMayNho_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureMayNho.Image;
            kind = 2;
            id = 0;
            filename = "Hinh\\maynho.gif";
        }

        private void pictureMayTo_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureMayTo.Image;
            kind = 3;
            id = 0;
            filename = "Hinh\\mayto.gif";
        }

        private void pictureCayCao_Click(object sender, EventArgs e)
        {
            id = 0;
            pictureIsSelect.Image = pictureCayCao.Image;
            kind = 4;
            filename = "Hinh\\caycao.gif";
        }

        private void pictureCayThap_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureCayThap.Image;
            id = 0;
            kind = 5;
            filename = "Hinh\\caythap.gif";
        }

        private void pictureDamCo_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureDamCo.Image;
            id = 0;
            kind = 6;
            filename = "Hinh\\damco.gif";
        }

        private void pictureLauDai_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureLauDai.Image;
            id = 0;
            kind = 7;
            filename = "Hinh\\laudai.png";
        }
        private void pictureGachDo_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureGachDo.Image;
            id = 1;
            kind = 8;
            filename = "Hinh\\gachdo.png";
        }
        private void pictureGachDoTien_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureGachDoTien.Image;
            id = 1;
            kind = 9;
            filename = "Hinh\\gachdo_chuatien.png";
        }
        private void pictureDoSao_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureDoSao.Image;
            id = 1;
            kind = 10;
            filename = "Hinh\\gachdo_star.png";
        }

        private void pictureDoNamMang_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureDoNamMang.Image;
            id = 1;
            kind = 11;
            filename = "Hinh\\gachdo_mushroom_do.png";
        }

        private void pictureGachXanh_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureGachXanh.Image;
            id = 1;
            kind = 12;
            filename = "Hinh\\gachxanh.png";
        }
        private void pictureGachXanhTien_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureGachXanhTien.Image;
            id = 1;
            kind = 13;
            filename = "Hinh\\gachxanh_chuatien.png";
        }

        private void pictureXanhSao_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureXanhSao.Image;
            id = 1;
            kind = 14;
            filename = "Hinh\\gachxanh_star.png";
        }
        private void pictureGachDauHoi_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureGachDauHoi_Nam.Image;
            id = 1;
            kind = 15;
            filename = "Hinh\\gachdauhoi_nam.png";
        }


        
        private void pictureGachHoiTien_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureGachHoiTien.Image;
            id = 1;
            kind = 16;
            filename = "Hinh\\gach_tien.png";
        }

        private void pictureGachCungDo_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureGachCungDo.Image;
            id = 1;
           
            kind = 17;
            filename = "Hinh\\gachcungdo.png";
        }

        private void pictureGachCungXanh_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureGachCungXanh.Image;
            id = 1;
            kind = 18;
            filename = "Hinh\\gachcungxanh.png";
        }
        private void pictureGachNen1_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureGachNen1.Image;
            id = 1;
            kind = 19;
            filename = "Hinh\\gachnen1.png";
        }

        private void pictureGachNen2_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureGachNen2.Image;
            id = 1;
            kind = 20;
            filename = "Hinh\\gachnen2.png";
        }
        private void pictureNapOng_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureNapOng.Image;
            kind = 21;
            id = 1;
            filename = "Hinh\\nap ong.png";
        }
        

        private void pictureNapOngDown_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureNapOngDown.Image;
            id = 1;
            kind = 22;
            filename = "Hinh\\nap ong_down.png";
        }
        
        private void pictureNapOngNgang_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureNapOngNgang.Image;
            id = 1;
            kind = 23;
            filename = "Hinh\\napongngang.png";
        }
        private void pictureThanOng_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureThanOng.Image;
            kind = 24;
            id = 1;
            filename = "Hinh\\than ong.png";
        }
        private void pictureThanOngNgang_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureThanOngNgang.Image;
            id = 1;
            kind = 25;
            filename = "Hinh\\thanongngang.png";
        }

        private void pictureCauKhi_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureCauKhi.Image;
            id = 1;
            kind = 26;
            filename = "Hinh\\caukhi.gif";
        }
        private void pictureCauKhiLen_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureCauKhiLen.Image;
            id = 1;
            kind = 27;
            filename = "Hinh\\CauKhiLen.png";
        }
       

        private void pictureRua_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureRua.Image;
            kind = 28;
            id = 2;
            filename = "Hinh\\ga.png";
           
        }
        private void pictureCayKeo_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureCayKeo.Image;
            id = 2;
            kind = 29;
            filename = "Hinh\\caykeo.png";
        }
        private void pictureNam_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureNam.Image;
            kind = 30;
            id = 2;
            filename = "Hinh\\Goomba.gif";
        }
       

        private void pictureTien_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureTien.Image;
            kind = 31;
            id = 3;
            filename = "Hinh\\tien.png";
        }
        private void pictureThanCotCo_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureThanCotCo.Image;
            id = 4;
            kind = 32;
            filename = "Hinh\\thanco.png";
        }
        private void pictureDinhCotCo_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureDinhCotCo.Image;
            id = 4;
            kind = 33;
            filename = "Hinh\\dinhco.png";
        }
        private void pictureLaCo_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureLaCo.Image;
            id = 4;
            kind = 34;
            filename = "Hinh\\co.png";
        }
        
        

        private void PanelBackground_MouseDown(object sender, MouseEventArgs e)
        {
            Object ob = new Object();
            ob.id = id;
            ob.kind = kind;
            ob.x = mousex + curposition;
            ob.y = mousey;
            ob.filename = filename;

            Image image = Image.FromFile(filename);
            Bitmap bmp = new Bitmap(image);
            ob.Width= bmp.Size.Width;
            ob.Height = bmp.Size.Height;
            listObject.Add(ob);
          //  isdrawed = true;
            radioButtonDelete.Enabled = true;
                
          
            
        }

        private void PanelBackground_MouseHover(object sender, EventArgs e)
        {
          //  draw_background.FillRectangle(Brushes.Blue,mousex,mousey, 32, 32);
            ismousein = true;
        }

        

        private void PanelBackground_MouseMove(object sender, MouseEventArgs e)
        {
            if ((kind >7&&kind<21)||(kind>29&&kind<33))
            {
                mousex = (e.X / 32) * 32;
                mousey = (e.Y / 32) * 32;
            }
            else
                if (kind<21||kind>25)
                {
                    mousex = e.X;
                    mousey = e.Y;

                }
                else
                    if (kind==25)
                    {
                        mousex = (e.X / 32) * 32;
                        mousey = e.Y;
                       
                    }
                    else
                    {
                        mousex = e.X;
                        mousey = (e.Y / 32) * 32;

                    }
            if (isdrawed == false)
            {
                for (int i = 0; i < listObject.Count; i++)
                {
                    Object ob = new Object();
                    ob = (Object)listObject[i];
                    if ((mousex + curposition == ob.x) && (mousey == ob.y) && (kind == ob.kind))
                    {
                        Delete();
                        if (isdelete == true) listObject.RemoveAt(i);
                        
                        return;
                    }
                   
                }

            }
           
        }

        private void PanelBackground_MouseLeave(object sender, EventArgs e)
        {
            ismousein = false;
        }
        private void Delete()
        {
           
                
                    try
                    {
                        string notify = "Are you sure that you want to delete this object ?";
                        if (MessageBox.Show(notify, "YES", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            isdelete = true;
                        }
                        else isdelete = false;
                            
                    }
                    catch (Exception eg)
                    {
                    }
                
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (newmap.YourBackground == false) draw_bitmap.Clear(newmap.BackgroundColor);
            else
                draw_bitmap.DrawImage(background, 0, 0, 640, 480);
            for (int i = 0; i < listObject.Count; i++)
            {
                Object ob = new Object();
                ob = (Object)listObject[i];
                if (ob.x >= curposition && ob.x < curposition + 640)
                {
                    Image imageobject = Image.FromFile(ob.filename);
                    Bitmap bmpobject = new Bitmap(imageobject);
                    draw_bitmap.DrawImage(bmpobject, ob.x-curposition, ob.y);
                }
            }
            if (ismousein == true)
            {
                Image image = Image.FromFile(filename);
                Bitmap bmp = new Bitmap(image);
                width = bmp.Size.Width;
                height = bmp.Size.Height;
                draw_bitmap.DrawImage(bmp, mousex,mousey);
            }
          //  else draw_bitmap.DrawRectangle(new Pen(Brushes.White), mousex, mousey, width,height);
            draw_background.DrawImage(Background, 0, 0);
            label1.Text = (curposition + 640).ToString();
            label2.Text = mapwidth.ToString();
           
          
        }

       
       

        private void pictureVuc_Click(object sender, EventArgs e)
        {
            pictureIsSelect.Image = pictureXanhSao.Image;
            kind = 8;
            id = 1;
            filename = "Hinh\\ho.png";
        }

        
        private void buttonNext_Click(object sender, EventArgs e)
        {
            curposition += 320;
          //  mapwidth += curposition;
            if(mapwidth<curposition+640)
            mapwidth += 320;
            buttonPrev.Enabled = true;
        }

        private void buttonPrev_Click(object sender, EventArgs e)
        {
            curposition -= 320;
            if (curposition == 0)
            {
                buttonPrev.Enabled = false;
            }
        }

        private void radioButtonDelete_CheckedChanged(object sender, EventArgs e)
        {
            isdrawed = false;
        }

        private void radioButtonDraw_CheckedChanged(object sender, EventArgs e)
        {
            isdrawed = true;
        }
        private void NewMap()
        {
            
            newmap.ShowDialog();
            if(newmap.YourBackground==true) background = new Bitmap(newmap.Background);
            //nObject = 0;
            listObject.Clear();
            timer1.Enabled = true;
            
            
        }
        private void SaveMap()
        {
            try
            {
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "Map files (*.MAP)|*.MAP";
                save.ShowDialog();
                string path = save.FileName;
                FileStream fs = File.Create(path, 50);
                fs.Close();
                StreamWriter sw = new StreamWriter(path);
                sw.WriteLine(mapwidth);
                sw.WriteLine(480);
                sw.WriteLine(listObject.Count);
               for (int i = 0; i < listObject.Count; i++)
                {
                    Object ob=new Object();
                   ob=(Object)listObject[i];
                    sw.WriteLine(ob.id);
                    sw.WriteLine(ob.kind);
                    sw.WriteLine(ob.x);
                    sw.WriteLine(ob.y);
                    sw.WriteLine(ob.Width);
                   sw.WriteLine(ob.Height);
                   // sw.WriteLine(ob.filename);
                }
                sw.Close();
            }
            catch (Exception eg)
            {
            }
            this.Focus();
            timer1.Enabled = true;
        }

       

        

       

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
           // timer1.Enabled = false;
            SaveMap();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenMap();
        }
        private void OpenMap()
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Map files (*.MAP)|*.MAP";
                open.ShowDialog();
                StreamReader reader;
                reader = new StreamReader(open.OpenFile());
                int w, h, n;
                w = int.Parse(reader.ReadLine());
                h = int.Parse(reader.ReadLine());
                n = int.Parse(reader.ReadLine());
                listObject.Clear();
                for (int i = 0; i < n; i++)
                {
                    Object ob = new Object();
                    ob.id = int.Parse(reader.ReadLine());
                    ob.kind = int.Parse(reader.ReadLine());
                    ob.x = int.Parse(reader.ReadLine());
                    ob.y = int.Parse(reader.ReadLine());
                    ob.Width = int.Parse(reader.ReadLine());
                    ob.Height = int.Parse(reader.ReadLine());
                  //  ob.filename = reader.ReadLine();
                    ob.filename = getFilename(ob.kind);
                    listObject.Add(ob);
                }
                reader.Close();

            }
            catch (Exception eg)
            {
            }
            timer1.Enabled = true;
        }
        private string getFilename(int kind)
        {
            string filename = "";
            switch (kind)
            {
                case 0: filename = "Hinh\\nuinho.gif"; break;
                case 1: filename = "Hinh\\nuito.gif"; break;
                case 2: filename = "Hinh\\maynho.gif"; break;
                case 3: filename = "Hinh\\mayto.gif"; break;
                case 4: filename = "Hinh\\caycao.gif"; break;
                case 5: filename = "Hinh\\caythap.gif"; break;
                case 6: filename = "Hinh\\damco.gif"; break;
                case 7: filename = "Hinh\\laudai.png"; break;
                case 8: filename = "Hinh\\gachdo.png"; break;
                case 9: filename = "Hinh\\gachdo_chuatien.png"; break;
                case 10: filename = "Hinh\\gachdo_star.png"; break;
                case 11: filename = "Hinh\\gachdo_mushroom_do.png"; break;
                case 12: filename = "Hinh\\gachxanh.png"; break;
                case 13: filename = "Hinh\\gachxanh_chuatien.png"; break;
                case 14: filename = "Hinh\\gachxanh_star.png"; break;
                case 15: filename = "Hinh\\gachdauhoi_nam.png"; break;
                case 16: filename = "Hinh\\gach_tien.png"; break;
                case 17: filename = "Hinh\\gachcungdo.png"; break;
                case 18: filename = "Hinh\\gachcungxanh.png"; break;
                case 19: filename = "Hinh\\gachnen1.png"; break;
                case 20: filename = "Hinh\\gachnen2.png"; break;
                case 21: filename = "Hinh\\nap ong.png"; break;
                case 22: filename = "Hinh\\nap ong_down.png"; break;
                case 23: filename = "Hinh\\napongngang.png"; break;
                case 24: filename = "Hinh\\than ong.png"; break;
                case 25: filename = "Hinh\\thanongngang.png"; break;
                case 26: filename = "Hinh\\caukhi.gif"; break;
                case 27: filename = "Hinh\\CauKhiLen.png"; break;
                case 28: filename = "Hinh\\ga.png"; break;
                case 29: filename = "Hinh\\caykeo.png"; break;
                case 30: filename = "Hinh\\Goomba.gif"; break;
                case 31: filename = "Hinh\\tien.png"; break;
                case 32: filename = "Hinh\\thanco.png"; break;
                case 33: filename = "Hinh\\dinhco.png"; break;
                case 34: filename = "Hinh\\co.png"; break;
               
            }
            return filename;
        }
        

       

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        
       

       


       








    }
}
